home *** CD-ROM | disk | FTP | other *** search
/ The Utilities Experience / The Utilities Experience - Volume 1.iso / software / misc / o-z / x-windows / gs262 / gsmain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-29  |  8.6 KB  |  302 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gsmain.c */
  20. /* Common support for Ghostscript front ends */
  21. #include "memory_.h"
  22. #include "string_.h"
  23. #include "ghost.h"
  24. #include "gp.h"
  25. #include "gsmatrix.h"            /* for gxdevice.h */
  26. #include "gxdevice.h"
  27. #include "gserrors.h"
  28. #include "estack.h"
  29. #include "ostack.h"            /* put here for files.h */
  30. #include "stream.h"            /* for files.h */
  31. #include "files.h"
  32. #include "iscan.h"
  33. #include "main.h"
  34. #include "store.h"
  35. #ifdef AMIGA
  36. #include <stdlib.h>
  37. #endif
  38.  
  39. /* ------ Exported data ------ */
  40.  
  41. uint gs_memory_chunk_size = 20000;
  42. int gs_user_errors;
  43.  
  44. /* File name search paths */
  45. const char **gs_lib_paths;
  46. private int gs_lib_count;
  47. char *gs_lib_env_path;
  48.  
  49. /* ------ Imported data ------ */
  50.  
  51. /* Configuration information imported from gconfig.c. */
  52. extern gx_device *gx_device_list[];
  53. extern const char *gs_lib_default_path;
  54. extern const char *gs_init_file;
  55. extern ref gs_init_file_array[];
  56.  
  57. /* Imported from gsmisc.c */
  58. extern int gs_log_errors;
  59. extern FILE *gs_debug_out;
  60.  
  61. /* ------ Forward references ------ */
  62.  
  63. private int gs_run_init_file(P2(int *, ref *));
  64. private int gs_run_file_open(P2(const char *, ref *));
  65.  
  66. #ifdef AMIGA
  67. /* ------ External references ------ */
  68. extern void alloc_init(const gs_memory_procs *, uint);
  69. extern void interp_init(void);
  70. extern void initial_enter_name(const char *, ref *);
  71. extern void alloc_restore_all(void);
  72. extern int zflush(register os_ptr);
  73. #endif
  74.  
  75. /* ------ Initialization ------ */
  76.  
  77. /* Remember how much initialization has been done. */
  78. private int init1_done, init2_done;
  79.  
  80. /* A handy way to declare and execute an initialization procedure: */
  81. #define call_init(proc)\
  82. { extern void proc(P0()); proc(); }
  83.  
  84. /* Initialization to be done before anything else. */
  85. void
  86. gs_init0(FILE *in, FILE *out, FILE *err, int max_lib_paths)
  87. {    /* Set the Ghostscript versions of stdin/out/err. */
  88.     gs_stdin = in;
  89.     gs_stdout = out;
  90.     gs_stderr = err;
  91.     gs_debug_out = gs_stdout;
  92.     /* Do platform-dependent initialization. */
  93.     /* We have to do this as the very first thing, */
  94.     /* because it detects attempts to run 80N86 executables (N>0) */
  95.     /* on incompatible processors. */
  96.     gp_init();
  97.     /* Initialize the file search paths */
  98.     gs_lib_env_path = 0;
  99.     gs_lib_paths =
  100.         (const char **)gs_malloc(max_lib_paths + 3, sizeof(char *),
  101.                      "gs_lib_paths array");
  102.     gs_lib_count = 0;
  103.     gs_user_errors = 1;
  104.     gs_log_errors = 0;
  105.     /* Reset debugging flags */
  106.     memset(gs_debug, 0, 128);
  107.     init1_done = init2_done = 0;
  108. }
  109.  
  110. /* Initialization to be done before constructing any objects. */
  111. void
  112. gs_init1(void)
  113. {    if ( !init1_done )
  114.        {    alloc_init(&gs_default_memory_procs, gs_memory_chunk_size);
  115.         call_init(name_init)
  116.         call_init(obj_init)        /* requires name_init */
  117.         call_init(scan_init)        /* ditto */
  118.         init1_done = 1;
  119.        }
  120. }
  121.  
  122. /* Initialization to be done before running any files. */
  123. void
  124. gs_init2(void)
  125. {    gs_init1();
  126.     if ( !init2_done )
  127.        {    int code, exit_code;
  128.         ref error_object;
  129.         call_init(igs_init)
  130.         call_init(zop_init)
  131.         interp_init();        /* requires obj_init */
  132.         call_init(op_init)    /* requires obj_init, scan_init */
  133.         /* Set up the array of additional initialization files. */
  134.         {    ref *ifp = gs_init_file_array;
  135.             ref ifa;
  136.             for ( ; ifp->value.bytes != 0; ifp++ )
  137.               r_set_size(ifp, strlen((const char *)ifp->value.bytes));
  138.             make_tasv(&ifa, t_array, a_readonly,
  139.                   ifp - gs_init_file_array, refs,
  140.                   gs_init_file_array);
  141.             initial_enter_name("INITFILES", &ifa);
  142.         }
  143.         /* Execute the standard initialization file. */
  144.         code = gs_run_init_file(&exit_code, &error_object);
  145.         if ( code < 0 )
  146.         {    if ( code != gs_error_Fatal )
  147.                 gs_debug_dump_stack(code, &error_object);
  148.             gs_exit_with_code((exit_code ? exit_code : 2), code);
  149.         }
  150.         init2_done = 1;
  151.        }
  152.    }
  153.  
  154. /* Add a library search path to the list. */
  155. void
  156. gs_add_lib_path(const char *lpath)
  157. {    gs_lib_paths[gs_lib_count] = lpath;
  158.     gs_lib_count++;
  159.     gs_set_lib_paths();
  160. }
  161.  
  162. /* ------ Execution ------ */
  163.  
  164. /* Complete the list of library search paths. */
  165. void
  166. gs_set_lib_paths(void)
  167. {    const char **ppath = &gs_lib_paths[gs_lib_count];
  168.     if ( gs_lib_env_path != 0 ) *ppath++ = gs_lib_env_path;
  169.     if ( gs_lib_default_path != 0 ) *ppath++ = gs_lib_default_path;
  170.     *ppath = 0;
  171. }
  172.  
  173. /* Open a file, using the search paths. */
  174. int
  175. gs_lib_open(const char *file_name, ref *pfile)
  176. {    /* This is a separate procedure only to avoid tying up */
  177.     /* extra stack space while running the file. */
  178. #define maxfn 200
  179.     byte fn[maxfn];
  180.     uint len;
  181.     return lib_file_open(file_name, strlen(file_name), fn, maxfn,
  182.                  &len, pfile);
  183. }
  184.  
  185. /* Open and execute a file. */
  186. int
  187. gs_run_file(const char *file_name, int user_errors, int *pexit_code, ref *perror_object)
  188. {    ref initial_file;
  189.     int code = gs_run_file_open(file_name, &initial_file);
  190.     if ( code < 0 ) return code;
  191.     return gs_interpret(&initial_file, user_errors, pexit_code, perror_object);
  192. }
  193. private int
  194. gs_run_file_open(const char *file_name, ref *pfref)
  195. {    gs_set_lib_paths();
  196.     if ( gs_lib_open(file_name, pfref) < 0 )
  197.     {    eprintf1("Can't find initialization file %s.\n", file_name);
  198.         return_error(gs_error_Fatal);
  199.     }
  200.     r_set_attrs(pfref, a_execute + a_executable);
  201.     return 0;
  202. }
  203.  
  204. /* Open and run the very first initialization file. */
  205. private int
  206. gs_run_init_file(int *pexit_code, ref *perror_object)
  207. {    ref initial_file;
  208.     ref first_token;
  209.     int code;
  210.     const char *file_name = gs_init_file;
  211.     /* Check to make sure the first token is an integer */
  212.     /* (for the version number check.) */
  213.     code = gs_run_file_open(file_name, &initial_file);
  214.     if ( code < 0 )
  215.         return code;
  216.     code = scan_token(initial_file.value.pfile, 0, &first_token);
  217.     if ( code < 0 || !r_has_type(&first_token, t_integer) )
  218.     {    eprintf1("Initialization file %s is not compatible with this interpreter.\n", file_name);
  219.         *pexit_code = 255;
  220.         return_error(gs_error_Fatal);
  221.     }
  222.     *++osp = first_token;
  223.     return gs_interpret(&initial_file, gs_user_errors, pexit_code, perror_object);
  224. }
  225.  
  226. /* Run a string. */
  227. int
  228. gs_run_string(const char *str, int user_errors, int *pexit_code, ref *perror_object)
  229. {    ref fref;
  230.     int code;
  231.     gs_set_lib_paths();
  232.     code = file_read_string((const byte *)str, (uint)strlen(str), &fref);
  233.     if ( code < 0 ) return code;
  234.     r_set_attrs(&fref, a_executable);
  235.     code = gs_interpret(&fref, user_errors, pexit_code, perror_object);
  236.     file_close(&fref);
  237.     return code;
  238. }
  239.  
  240. /* ------ Termination ------ */
  241.  
  242. /* Free all resources and exit. */
  243. void gs_malloc_release(P0());
  244. int gs_closedevice(P1(gx_device *));
  245. void
  246. gs_finit(int exit_status, int code)
  247. {    gx_device **pdev = gx_device_list;
  248.     fflush(stderr);            /* in case of error exit */
  249.     for ( ; *pdev != 0; pdev++ )
  250.         gs_closedevice(*pdev);
  251.     /* Do the equivalent of a restore "past the bottom". */
  252.     /* This will release all memory, close all open files, etc. */
  253.     if ( init1_done )
  254.         alloc_restore_all();
  255.     /* Do platform-specific cleanup. */
  256.     gp_exit(exit_status, code);
  257. }
  258. void
  259. gs_exit_with_code(int exit_status, int code)
  260. {    gs_finit(exit_status, code);
  261.     exit(exit_status);
  262. }
  263. void
  264. gs_exit(int exit_status)
  265. {    gs_exit_with_code(exit_status, 0);
  266. }
  267.  
  268. /* ------ Debugging ------ */
  269.  
  270. /* Debugging code */
  271. extern void debug_print_ref(P1(const ref *));
  272. extern void debug_dump_refs(P3(const ref *, uint, const char *));
  273.  
  274. /* Dump the stacks after interpretation */
  275. void
  276. gs_debug_dump_stack(int code, ref *perror_object)
  277. {    zflush(osp);    /* force out buffered output */
  278.     dprintf1("\nUnexpected interpreter error %d.\n", code);
  279.     if ( perror_object != 0 )
  280.     {    dputs("Error object: ");
  281.         debug_print_ref(perror_object);
  282.         dputc('\n');
  283.     }
  284.     debug_dump_refs(osbot, osp + 1 - osbot, "Operand stack");
  285.     debug_dump_refs(esbot, esp + 1 - esbot, "Execution stack");
  286. }
  287.  
  288. /* Log an error return */
  289. #ifdef DEBUG
  290. int
  291. gs_log_error(int err, const char _ds *file, int line)
  292. {    if ( gs_log_errors )
  293.       { if ( file == NULL )
  294.           dprintf1("Returning error %d.\n", err);
  295.         else
  296.           dprintf3("%s(%d): Returning error %d.\n",
  297.                (char *)file, line, err);
  298.       }
  299.     return err;
  300. }
  301. #endif
  302.